package com.pdweb.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pdweb.dao.EmpresasDao;
import com.pdweb.vo.Empresas;

/**
 * Servlet implementation class EmpresaAdicionaServlet
 */
@WebServlet("/EmpresaAdicionaServlet")
public class EmpresaAdicionaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmpresaAdicionaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doService(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		doService(request, response);
		
	}
	
	protected void doService(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		try{
			Empresas empresa = new Empresas();
			
			empresa.setNome(request.getParameter("ST_NOME_EMP"));
			empresa.setNomeFantasia(request.getParameter("ST_FANTASIA_EMP"));
			empresa.setCnpj(request.getParameter("ST_CNPJ_EMP"));
			empresa.setRua(request.getParameter("ST_RUAEND_EMP"));
			empresa.setBairro(request.getParameter("ST_BAIRRO_EMP"));
			empresa.setCep(Integer.parseInt(request.getParameter("NM_CEP_EMP")));
			empresa.setTelefone(Integer.parseInt(request.getParameter("NM_TELEFONE_EMP")));
			empresa.setEmail(request.getParameter("ST_EMAIL_EMP"));
			empresa.setPagina(request.getParameter("ST_URL_EMP"));
			empresa.setNumero(Integer.parseInt(request.getParameter("NM_NUMEROEND_EMP")));
			empresa.setCidade(request.getParameter("ST_CIDADE_EMP"));
			empresa.setUf("ST_UF_EMP");
			
			EmpresasDao empresasDao = new EmpresasDao();
				
			empresasDao.adicionaEmpresa(empresa);
			
			getServletContext().getRequestDispatcher("/EmpresasExibirServlet").forward(request,response);
			
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

}
